#!/bin/bash
echo "hello, world 大师傅似" > origin

for file in uk rk ciphertext decipher signedfile dhuk1 dhrk1 dhuk2 dhrk2 secure1 secure2; do
    if [ -s $file ]; then
        rm $file
    fi
done

./test2 rsakey uk rk >> tmp.tmp
if [ ! -s uk ] || [ ! -s rk ]; then
echo "failed on rsa key generation"
exit 1
else 
echo "success on rsa key generation"
fi

./test2 encrypt uk origin ciphertext >> tmp.tmp
if [ ! -s ciphertext ]; then
echo "went wrong when encrypting"
exit 1
fi

./test2 decrypt rk ciphertext decipher >> tmp.tmp
if [ ! -s decipher ]; then
echo "went wrong when decrypting"
exit 1
fi
result=$(diff origin decipher -q)
if [ ! -z $result ]; then
echo "failure on encryption and decryption"
exit 1
else 
echo "success on encryption and decryption"
fi

./test2 sign rk origin signedfile >> tmp.tmp
if [ ! -s signedfile ]; then
echo "went wrong when signing"
exit 1
fi
result=`./test2 verify uk origin signedfile`
# echo "result is $result"
A="succedd"
# if [[ $result == *$A* ]]
if [[ $result =~ $A ]]
then
  echo "success on sigature and verification"
else
  echo "failure on sigature and verification"
fi

./test2 dhkey dhuk1 dhrk1 >> tmp.tmp
./test2 dhkey dhuk2 dhrk2 >> tmp.tmp
./test2 dhsecure dhrk1 dhuk2 secure1 >> tmp.tmp
./test2 dhsecure dhrk2 dhuk1 secure2 >> tmp.tmp
result=$(diff secure1 secure2 -q)
if [ ! -z $result ]; then
echo "failure on Diffe-Hellman key exchange"
exit 1
else 
echo "success on Diffe-Hellman key exchange"
fi